package com.cw.jeeyt.service.impl.facade;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.bo.facade.FacadeProjectBo;
import com.cw.jeeyt.service.converter.facade.FacadeProjectConverter;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.facade.FacadeProjectDao;
import com.cw.jeeyt.dao.model.facade.FacadeProjectDo;
import com.cw.jeeyt.dao.query.facade.FacadeProjectQuery;
import com.cw.jeeyt.service.facade.FacadeProjectService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 项目信息接口业务实现类
 *
 * @author chenw
 * @date 2019-11-01 23:46:50
 */
@Service
public class FacadeProjectServiceImpl implements FacadeProjectService {
    @Autowired
    private FacadeProjectDao facadeProjectDao;

    /**
     * 新增项目信息
     *
     * @param request 条件
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addFacadeProject(FacadeProjectBo request) {
        if (request == null) {
            return null;
        }
        request.setCreateBy(SessionUtil.getSessionUserId());
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setCreateDate(new Date());
        request.setUpdateDate(new Date());
        facadeProjectDao.insertBySelective(FacadeProjectConverter.bo2Do(request));
        return "";
    }

    /**
     * 更新项目信息
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyFacadeProject(FacadeProjectBo request) {
        if (request == null) {
            return false;
        }
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setUpdateDate(new Date());
        facadeProjectDao.updateBySelective(FacadeProjectConverter.bo2Do(request));
        return true;
    }

    /**
     * 删除项目信息
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFacadeProject(FacadeProjectBo request) {
        if (request == null) {
            return false;
        }
        return facadeProjectDao.deleteByKey(request.getId()) > 0;
    }

    /**
     * 分页查询项目信息
     *
     * @param request 条件
     * @return PageInfo<FacadeProjectBo>
     */
    @Override
    public PageInfo<FacadeProjectBo> selectPage(FacadeProjectBo request, PageParam pageParam) {
        PageInfo<FacadeProjectDo> pageInfo = facadeProjectDao.selectPage(FacadeProjectQuery.builder()
            .id(request.getId())
            .projectCode(request.getProjectCode())
            .projectName(request.getProjectName())
            .projectComment(request.getProjectComment())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build(), pageParam);
        List<FacadeProjectBo> facadeProjects = FacadeProjectConverter.doList2BoList(pageInfo.getList());
        return new PageInfo<>(pageInfo, facadeProjects);
    }

    /**
     * 查询项目信息详情信息
     *
     * @param request 条件
     * @return FacadeProjectBo
     */
    @Override
    public FacadeProjectBo queryFacadeProjectDetail(FacadeProjectBo request) {
        if (request == null) {
            return null;
        }
        return FacadeProjectConverter.do2Bo(facadeProjectDao.selectByKey(request.getId()));
    }

    /**
     * 根据条件查询项目信息
     *
     * @param request 条件
     * @return List<FacadeProjectBo>
     */
    @Override
    public List<FacadeProjectBo> queryFacadeProjectList(FacadeProjectBo request) {
        if (request == null) {
            return null;
        }
        return FacadeProjectConverter.doList2BoList(facadeProjectDao.selectByExample(FacadeProjectQuery.builder()
            .id(request.getId())
            .projectCode(request.getProjectCode())
            .projectName(request.getProjectName())
            .projectComment(request.getProjectComment())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build()));
    }

    /**
     * 根据key查询项目信息
     *
     * @param id
     * @return
     */
    @Override
    public FacadeProjectBo selectByKey(Long id) {

        return FacadeProjectConverter.do2Bo(facadeProjectDao.selectByKey(id));
    }
}
